﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Text;
using System.Security;
using System.Web.Security;

using Webchatting.Dal;
using System.Configuration;
using System.Data.OleDb;
using System.Web;
using Webchatting.Common;
using System.Collections;

namespace Webchatting.Dal 
{
    public class OperationRU 
    {
        #region 变量
		 public static readonly string connStr = (ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
      
         public SqlConnection ConnSql
            {
                    set;
                    get;
             }
	    #endregion

        public OperationRU()
        {
            this.ConnSql = new SqlConnection(connStr);
        }
      
        //注册用户
        public int AddRUser(string RUId, string PassWord, string RUNickName, string RUName, string RUSex, string RUSchool, string RUEmail, 
            string RUCity, string RUAge, string RUAddress,string RUPhonenum)
        {
            //TODO
            int Id = int.Parse(RUId);
            int Age = int.Parse(RUAge);
            
            SqlParameter[] sqlParams = new SqlParameter[11];
            sqlParams[0] = new SqlParameter("@RUId", Id);
            sqlParams[1] = new SqlParameter("@PassWord", PassWord);
            sqlParams[2] = new SqlParameter("@RUNickName", RUNickName);
            sqlParams[3] = new SqlParameter("@RUName", RUName);
            sqlParams[4] = new SqlParameter("@RUSex", RUSex);
            sqlParams[5] = new SqlParameter("@RUSchool", RUSchool);
            sqlParams[6] = new SqlParameter("@RUEmail", RUEmail);
            sqlParams[7] = new SqlParameter("@RUCity", RUCity);
            sqlParams[9] = new SqlParameter("@RUAge", Age);
            sqlParams[8] = new SqlParameter("@RUAddress", RUAddress);
            sqlParams[10] = new SqlParameter("@RUPhonenum", RUPhonenum);
            

           
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure, "spAddRUser", sqlParams);
            return 1;
        }
       
        //登录验证
        public int CheckLogin(string RUId, string Password)
        {
           
             SqlParameter[] sqlParams = new SqlParameter[2]{
              new SqlParameter("@RUId", RUId),
              new SqlParameter("@Password",Password)
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql ,"spCheckLogin", sqlParams);

            
            IList<string> arr = new List<string>();
           
            while (dr.Read())
            {
                arr.Add(dr["RUId"].ToString().Trim());
                arr.Add(dr["RUNickname"].ToString().Trim());
                arr.Add(dr["RUPortrait"].ToString().Trim());
                arr.Add(dr["RUDes"].ToString().Trim());
                arr.Add(dr["IsAdmin"].ToString().Trim());
                
            }
            
            dr.Close();
            
           if (arr.Count == 5)
            {
                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, (arr[3].ToString() == "True" ? "Admin" : "Users"), DateTime.Now, DateTime.Now.AddMinutes(60), false, arr[0].ToString() + "@" + arr[1].ToString() + "@" + arr[2].ToString() + "@" + arr[3].ToString() + "@" + arr[4].ToString()); //建立身份验证票对象
                string HashTicket = FormsAuthentication.Encrypt(Ticket); //加密序列化验证票为字符串

                HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
                UserCookie.HttpOnly = true;
                //生成Cookie
                HttpContext.Current.Response.Cookies.Add(UserCookie); //输出Cookie

                HttpContext.Current.Response.Redirect("server/RUIndex.aspx", false); // 定位到管理页面
            }
            return 1;
        }

       //获取头像
        public IList<string> GetRUPortrait(string RUId)
        {
            IList<string> ilist = new List<string>();
            SqlParameter[] sqlParams = new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@RUId", RUId);
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT * FROM RUser WHERE RUId=" + RUId);
            while (dr.Read())
            {
                ilist.Add(dr["RUId"].ToString().Trim());
                ilist.Add(dr["RUNickname"].ToString().Trim());
                ilist.Add(dr["RUPortrait"].ToString().Trim());
                ilist.Add(dr["RUDes"].ToString().Trim());
            }
            dr.Close();
            ConnSql.Close();
            return ilist;
        }
      
        //编辑头像、昵称、签名档
        public int EditRUPortrait(string RUId, string RUPortrait, string RUNickName,string RUDes)
        {
            
            SqlParameter[] sqlParams = new SqlParameter[4];
            sqlParams[0] = new SqlParameter("@RUId", RUId);
            sqlParams[1] = new SqlParameter("@RUPortrait", RUPortrait);
            sqlParams[2] = new SqlParameter("@RUNickName", RUNickName);
            sqlParams[3] = new SqlParameter("@RUDes", RUDes);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
                "spUpdatePortrait", sqlParams);
            
            ConnSql.Close();
            return 1;
        }
       
        //获取个人基本信息
        public IList<string> GetRUPersonInfo(string RUId)
        {
            IList<string> ilist = new List<string>();
            SqlParameter[] sqlParams = new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@RUId", RUId);
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT * FROM RUser WHERE RUId=" + RUId);
            while (dr.Read())
            {
                
                ilist.Add(dr["RUNickName"].ToString().Trim());
                ilist.Add(dr["RUName"].ToString().Trim());
                ilist.Add(dr["RUSchool"].ToString().Trim());
                ilist.Add(dr["RUEmail"].ToString().Trim());
                ilist.Add(dr["RUCity"].ToString().Trim());
                ilist.Add(dr["RUAddress"].ToString().Trim());
                ilist.Add(dr["RUAge"].ToString().Trim());
                ilist.Add(dr["RUPhoneNum"].ToString().Trim());
            }
            dr.Close();
            ConnSql.Close();
            return ilist;
        }
        //修改基本信息
        public int UpdatePersonInfo(string RUId,string RUNickName,string RUName,string RUSchool,string RUEmail,string RUCity,string RUAddress,string RUAge,string RUPhoneNum)
        {
            SqlParameter[] sqlParams = new SqlParameter[9];
            sqlParams[0] = new SqlParameter("@RUNickName", RUNickName);
            sqlParams[1] = new SqlParameter("@RUName", RUName);
            sqlParams[2] = new SqlParameter("@RUSchool", RUSchool);
            sqlParams[3] = new SqlParameter("@RUEmail", RUEmail);
            sqlParams[4] = new SqlParameter("@RUCity", RUCity);
            sqlParams[5] = new SqlParameter("@RUAddress", RUAddress);
            sqlParams[6] = new SqlParameter("@RUAge", RUAge);
            sqlParams[7] = new SqlParameter("@RUPhoneNum", RUPhoneNum);
            sqlParams[8] = new SqlParameter("@RUId", RUId);

            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
               "spUpdatePersonalBaseInfo", sqlParams);
            ConnSql.Close();
            return 1;

        }
       
        //注册时，将用户与密码加入安全信息
        public int InsertSafeInfo(string RUId, string password) 
        {
            SqlParameter[] sqlParams = new SqlParameter[2];
            sqlParams[0] = new SqlParameter("@RUId", RUId);
            sqlParams[1] = new SqlParameter("@Password",password);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
               "spInsertSafeInfo", sqlParams);
            return 1;
        }
        //获取安全信息
        public IList<string> GetRUSafeInfo(string RUId)
        {
            IList<string> ilist = new List<string>();
            SqlParameter[] sqlParams = new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@RUId", RUId);
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT * FROM RUSafeInfo WHERE RUId=" + RUId);
            while (dr.Read())
            {

                ilist.Add(dr["Password"].ToString().Trim());        
                ilist.Add(dr["RUSafeQue"].ToString().Trim());
                ilist.Add(dr["RUSafeAns"].ToString().Trim());
               
            }
            dr.Close();
            ConnSql.Close();
            return ilist;
        }
        //修改安全信息
        public int UpdateRUSafeInfo(string RUId, string Que, string Ans, string Pwd) 
        {
            SqlParameter[] sqlParams = new SqlParameter[4];
            sqlParams[0] = new SqlParameter("@Password", Pwd);
            sqlParams[1] = new SqlParameter("@RUSafeQue", Que);
            sqlParams[2] = new SqlParameter("@RUSafeAns", Ans);
            sqlParams[3] = new SqlParameter("@RUId", RUId);;
            SqlParameter[] sqlParams2 = new SqlParameter[2];
            sqlParams2[0] = new SqlParameter("@Password", Pwd);
            
            sqlParams2[1] = new SqlParameter("@RUId", RUId);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
            "spUpdateRUPwd", sqlParams2);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
             "spUpdateRUSafeInfo", sqlParams);
            ConnSql.Close();
            return 1;
        }

        //添加注册时间信息 以后改为记录离线时间  根据时间读取群消息
        public int InsertOnlineInfo(string RUId, DateTime Time) 
        {
            SqlParameter[] sqlParams = new SqlParameter[2];
            sqlParams[0] = new SqlParameter("@RUId", RUId);
            sqlParams[1] = new SqlParameter("@RUOnlineTime", Time);
            return SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
            "spInsertRUONlineInfo", sqlParams);
        }

        public int UpdateOnlineInfo(string RUId)
        {
            SqlParameter[] sqlParams = new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@RUId", RUId);
            
            return SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
            "spUpdateLeaveTime", sqlParams);
        }

        //获得最后的在线时间
        public string GetOnlineInfo(string RUId)
        {
            
            string LastOnlineTime = null ;
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT [RUOnlineTime] FROM ChattingDB.dbo.RUOnlineInfo WHERE  RUId ='" + RUId+"'");
            while (dr.Read())
            {

                LastOnlineTime = dr["RUOnlineTime"].ToString();

            }
            dr.Close();
            ConnSql.Close();
            return LastOnlineTime;
        }
    }
     
}